package pers.dawnyang.config.filter.decoder;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pers.dawnyang.common.util.JsonUtil;

/**
 * 
 * @author dawn yang
 * 
 */
public class DecoderRequestWrapper extends HttpServletRequestWrapper {
  private static final Logger logger = LoggerFactory.getLogger(DecoderRequestWrapper.class);

  private static String aseKey = "1234123412ABCDEF";

  private Map<String, String[]> params = new HashMap<String, String[]>();

  /*
   * public DecoderRequestWrapper(HttpServletRequest request, Map<String, String[]> params) {
   * super(request); if (BeanUtil.isEmpty(params)) { //
   * Map不能进行写操作（request的ParameterMap赋值给当前对象的Map，lock后不能进行写操作） this.params =
   * request.getParameterMap(); } else { this.params = params; } }
   */

  public DecoderRequestWrapper(HttpServletRequest request) {
    super(request);
  }


  /*
   * @Override public String getParameter(String name) { String strParameter =
   * super.getParameter(name); if (StringUtils.isEmpty(strParameter)) { return strParameter; }
   * System.out.println("getParameter:" + super.getParameter(name)); return
   * super.getParameter(name); }
   * 
   * @Override public Map<String, String[]> getParameterMap() { Map<String, String[]> params = new
   * HashMap<String, String[]>(); Map<String, String[]> parameterMap = super.getParameterMap();
   * System.out.println("flag1:" + parameterMap); params.putAll(super.getParameterMap()); String
   * estxt = super.getParameter("estxt"); if (StringUtils.isNotBlank(estxt)) { String requestParam =
   * null; String encodeHexStr = HexUtil.encodeHexStr(aseKey); byte[] decodeHex =
   * HexUtil.decodeHex(encodeHexStr); AES aes = SecureUtil.aes(decodeHex); requestParam =
   * aes.decryptStr(estxt); System.out.println("requestParam:" + requestParam);
   * params.putAll(changeToFormDateType(dealRequestParamStr(requestParam)));
   * System.out.println("flag2:" + changeToFormDateType(dealRequestParamStr(requestParam)));
   * params.remove("estxt"); String[] falg11 = new String[] {"1"}; params.put("flag11", falg11);
   * System.out.println("flag3:" + params); } params.remove("userCode"); return
   * Collections.unmodifiableMap(params);
   * 
   * }
   */

  private Map<String, Object> dealRequestParamStr(String requestParamStr) {
    Map<String, Object> requestDealMap = new HashMap<String, Object>();
    if (requestParamStr.indexOf("&") > 0) {
      requestParamStr = requestParamStr.replaceAll("\"", "");
      try {
        requestParamStr = URLDecoder.decode(requestParamStr, "utf-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      String[] paramArr = requestParamStr.split("&");
      for (String everyParam : paramArr) {
        String[] temp = everyParam.split("=");
        String paramName = temp[0];
        String paramValue = "";
        if (temp.length > 1) {
          paramValue = temp[1];
        }
        requestDealMap.put(paramName, paramValue);
      }
      return requestDealMap;
    }
    return JsonUtil.fromJson(requestParamStr);
  }


  private Map<String, String[]> changeToFormDateType(Map<String, Object> params) {
    Map<String, String[]> newMap = new HashMap<>();
    for (Map.Entry<String, Object> mapStr : params.entrySet()) {
      String value = "";
      if (null != mapStr.getValue()) {
        value = mapStr.getValue().toString();
      }
      if (StringUtils.isNotBlank(value)) {
        newMap.put(mapStr.getKey(), new String[0]);
        continue;
      }
      String[] sVal = value.split("::");
      newMap.put(mapStr.getKey(), sVal);
    }
    return newMap;
  }


  public void addAllParameters(Map<String, Object> otherParams) {// 增加多个参数
    for (Map.Entry<String, Object> entry : otherParams.entrySet()) {
      addParameter(entry.getKey(), entry.getValue());
    }
  }

  public void addParameter(String name, Object value) {// 增加参数
    if (value != null) {
      if (value instanceof String[]) {
        params.put(name, (String[]) value);
      } else if (value instanceof String) {
        params.put(name, new String[] {(String) value});
      } else {
        params.put(name, new String[] {String.valueOf(value)});
      }
    }
  }

}
